package com.orangelife.mobile.redeem.activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.curry.android.http.HttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.android.util.ImageDownloader;
import com.curry.android.util.ImageDownloader.ImageSize;
import com.curry.android.util.JSONHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.app.application.ExitApplication;
import com.orangelife.mobile.buyFresh.activity.BuyOrRegisterActivity;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.Constant;
import com.thirdsdk.umeng.UmShareBiz;
import com.orangelife.mobile.util.ToastHelper;
import com.orangelife.mobile.widget.BuyOrRegisterPopupWindow;
import com.orangelife.mobile.web.WebWin;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RedeemDetailActivity extends OrangeLifeBaseActivity {
	/** 标题中间 */
	private TextView tvMenubarTitle;
	/** 标题右边 */
	private TextView tvMenubarTitleRight;
	/** 图片下标题 */
	private TextView tvTitle;
	/** 图片下子标题*/
	private TextView tvChildTitle;
	/** 商品说明*/
	private TextView tvDescription;
	/** 现价*/
	private TextView tvPriceDiscount;
	/** 原价*/
	private TextView tvPrice;
	/** 规格*/
	private TextView tvWeight;
	/** 购买点击*/
	private TextView tvBuy;
	/** 图片webwin*/
	private WebWin wwPhoto;
	/** 分享*/
	private ImageButton ibShare;
	/** 购买弹出框 */
	BuyOrRegisterPopupWindow selectWindow;
	/** 选择规格点击*/
	private LinearLayout llSelectSpecification;
	/** 进度条 */
	private Dialog dialog = null;
	/** 分享title */
	private String uMtitle = "";
	/** 分享content */
	private String uMcontent = "";
	private String merId;
	/**接收从listview传过来的数据*/
	private Map<String,Object> map = null;
	/**大图片*/
	private ImageView ivBigPhoto;
	/**接收从后来传过来的数据*/
	private Map<String,Object> mapDetail = null;
	/** 规格*/
	private List<Map<String,Object>> listSpecification;
	private int widthPixels =0;
	private int heightPixels =0;
	/**图片下载*/
	private ImageDownloader image = ImageDownloader.getImageDownloader();
	/** 分享url */
	UmShareBiz shareBiz;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_buy_fresh_detail);
		shareBiz = new UmShareBiz(this);
		initeView();
		findView();
	}
	
	/**
	 * 初始化
	 */
	private void initeView(){
		tvMenubarTitle = (TextView)findViewById(R.id.tv_title);
		tvMenubarTitleRight = (TextView)findViewById(R.id.tv_title_right);
		tvTitle = (TextView)findViewById(R.id.tvTitle);
		tvChildTitle = (TextView)findViewById(R.id.tvChildTitle);
		tvDescription = (TextView)findViewById(R.id.tvTradeDescription);
		ibShare = (ImageButton)findViewById(R.id.ibShare);
		tvPriceDiscount = (TextView)findViewById(R.id.tvBuyPriceDiscount);
		tvPrice = (TextView)findViewById(R.id.tvBuyPrice);
		tvWeight = (TextView)findViewById(R.id.tvBuyWeight);
		tvBuy = (TextView)findViewById(R.id.tvParticipate);
		ivBigPhoto = (ImageView)findViewById(R.id.ivBigPhoto);
		wwPhoto = (WebWin)findViewById(R.id.wwPhoto);
		llSelectSpecification = (LinearLayout)findViewById(R.id.llSelectSpecification);
		tvBuy.setText("兑换");
		tvPrice.setVisibility(View.GONE);
		tvWeight.setVisibility(View.GONE);
	}
	/**
	 *赋值 
	 */
	private void findView(){
		tvMenubarTitle.setText(R.string.trade_detail);
		tvMenubarTitleRight.setVisibility(View.INVISIBLE);
		ibShare.setOnClickListener(listener);
		tvBuy.setOnClickListener(listener);
		llSelectSpecification.setOnClickListener(listener);
		
		
		Intent intent = getIntent();
		map = (Map<String, Object>) intent.getSerializableExtra("detail");
		getDataFromServer();
		getDetail();
	}
	
	/**
	 * 设置详情
	 */
	@SuppressWarnings("unchecked")
	private void setDetail(){
		DisplayMetrics mDisplayMetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
		widthPixels=mDisplayMetrics.widthPixels;
		heightPixels=(int)(widthPixels*(3/4f));
		LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(
	    		widthPixels, heightPixels);
	    ivBigPhoto.setLayoutParams(mParams);
		uMtitle = mapDetail.get("goodsName").toString();/* 店铺名称 */
		uMcontent = mapDetail.get("goodsDesc").toString();/* 店铺名称 */
		merId = mapDetail.get("goodsID").toString();
		tvTitle.setText(mapDetail.get("goodsName").toString());
		tvChildTitle.setText(mapDetail.get("goodsBrief").toString());
		tvDescription.setText(mapDetail.get("goodsDesc").toString());
		listSpecification = (List<Map<String, Object>>) mapDetail.get("products");
		tvPriceDiscount.setText(listSpecification.get(0).get("score").toString()+"积分");
//		tvPrice.setText("￥"+listSpecification.get(0).get("mkPrice").toString());
//		tvPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
//		tvWeight.setText(listSpecification.get(0).get("pdtDesc").toString());
		image.download(mapDetail.get("goodsLogo").toString().trim(), ivBigPhoto,
				ImageSize.IMAGE_SIZE_MIDDLE_JPG);
		setWebView();
	}
	/**
	 * 设置照片的webview
	 */
	private void setWebView() {
		String pics = mapDetail.get("goodsImgs").toString();
		wwPhoto.loadDataWithBaseURL(pics);
	}
	
	/**
	 * 下拉刷新的进度条
	 */
	private void getDataFromServer() {
		dialog = DialogHelper.getInstance().createLoadingDialog(this, Constant.LOADING);
		dialog.show();
	}
	
private OnClickListener listener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.ibShare:
				shareBiz.share(uMtitle,uMcontent,Constant.BUYFRESH_URL,merId);
				break;
			case R.id.tvParticipate:
				ExitApplication.getInstance().addFailActivity(RedeemDetailActivity.this);
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				intent.putExtra("active", 1);
				intent.putExtra("score", true);
				bundle.putSerializable("specification", (Serializable) mapDetail);
				if(mapDetail.size()>0){
					intent.putExtras(bundle);
					intent.setClass(RedeemDetailActivity.this, BuyOrRegisterActivity.class);
					startActivity(intent);
				}
				break;
			case R.id.llSelectSpecification:
				ExitApplication.getInstance().addFailActivity(RedeemDetailActivity.this);
				intent = new Intent();
				bundle = new Bundle();
				intent.putExtra("score", true);
				intent.putExtra("active", 1);
				bundle.putSerializable("specification", (Serializable) mapDetail);
				intent.putExtras(bundle);
				intent.setClass(RedeemDetailActivity.this, BuyOrRegisterActivity.class);
				startActivity(intent);
				break;

			default:
				break;
			}
		}
	};
	

	private void getDetail(){
		Map<String, Object> mapSubmit = new HashMap<String, Object>();
		String goodsID = null;
		if(map != null && map.size() != 0){
			goodsID = map.get("goodsID").toString();
		}else{
			goodsID = getIntent().getStringExtra("id");
		}
		mapSubmit.put("{goodsID}", goodsID);
		Map<String, Object> rootMap = new HashMap<String, Object>();
		rootMap.put("map", mapSubmit);
		rootMap.put("url", Constant.URL_BUYFRESH_DETAIL);
		rootMap.put("wat", 6);
		new JSONRequest(JSONRequest.GET, rootMap, handler, HttpUtil.ACTION_URI);
	}
	
	/**
	 * 处理返回的信息数据
	 */
	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 500:
				DialogHelper.closeDialog(dialog);
				ToastHelper.getInstance()._toast(
						msg.obj.toString());
				break;
			case 1011:
				isLogin(dialog);
				break;
			case 6:
				DialogHelper.closeDialog(dialog);
				mapDetail = new HashMap<String, Object>();
				if(!(JSONHelper.jsonToMap(
						msg.obj.toString()).get("entity").equals(""))){
					mapDetail = (Map<String, Object>) JSONHelper.jsonToMap(
							msg.obj.toString()).get("entity");
					setDetail();
				}
				break;
			default:
				break;
			}
		}

	};

}
